package handler

import (
	"api-getaway/api/register"
	"api-getaway/basic/model"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"net/http"
)

func Membership(r *gin.Context) {
	var req register.Membership
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}

	membership := model.Membership{
		UserId:     req.UserId,
		Membership: req.Membership,
		Duration:   req.Duration,
	}

	if err = membership.CreateMembership(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "会员等级管理创建失败",
		})
		return
	}
	r.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "会员等级管理创建成功",
		"data": membership,
	})

}

func Order(r *gin.Context) {
	var req register.Order
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}

	order := model.Order{
		UserId:       req.UserId,
		MembershipId: req.MembershipId,
		OrderNumber:  uuid.NewString(),
		Price:        req.Price,
	}

	if err = order.CreateOrder(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "订单生成失败",
		})
		return
	}
	r.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "订单生成成功",
		"data": order,
	})

}

func PayList(r *gin.Context) {
	var req register.Pay
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}

	pay := model.Pay{}
	if err = pay.FindPay(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}
	r.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": pay,
	})
}

func Pay(r *gin.Context) {
	var req register.Pay
	err := r.ShouldBind(&req)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  err.Error(),
		})
		return
	}

	pay := model.Pay{
		UserId:       req.UserId,
		MembershipId: req.MembershipId,
		TotalAmount:  req.TotalAmount,
		Status:       req.Status,
	}
	if err = pay.CreatePay(); err != nil {
		r.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "激活失败",
		})
		return
	}
	r.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "激活成功",
		"data": pay,
	})
}
